package com.rceeslca.modules.cam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rceeslca.common.exception.RceesLcaException;
import com.rceeslca.common.service.impl.BaseServiceImpl;
import com.rceeslca.modules.cam.dao.CamMajorDao;
import com.rceeslca.modules.cam.entity.CamMajor;
import com.rceeslca.modules.cam.service.CamMajorService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 专业
 */
@Service
@AllArgsConstructor
public class CamMajorServiceImpl extends BaseServiceImpl<CamMajorDao, CamMajor> implements CamMajorService {

    /**
     * 创建专业
     * @param major 专业
     */
    @Override
    public void createMajor(CamMajor major) {
        insert(major);
    }


    /**
     * 删除专业
     * @param major 专业
     */
    @Override
    public void deleteMajor(CamMajor major) {
        //存在
        CamMajor entity = baseDao.selectById(major.getId());
        if(entity==null){
            throw new RceesLcaException("数据不存在");
        }else{
            deleteById(major);
        }
    }


    /**
     * 更改专业
     * @param major 专业
     */
    @Override
    public void updateMajor(CamMajor major) {
        //存在
        CamMajor entity = baseDao.selectById(major.getId());
        if(entity==null){
            throw new RceesLcaException("数据不存在");
        }else{
            entity.setName(major.getName());
            entity.setDescription(major.getDescription());
            entity.setUpdater(major.getUpdater());
            entity.setUpdateDate(major.getUpdateDate());
            updateById(entity);
        }
    }


    /**
     * 列展示
     * @param param 参数
     */
    @Override
    public List<CamMajor> listMajor(CamMajor param) {
        //构造查询条件
        QueryWrapper<CamMajor> queryWrapper = new QueryWrapper<>();
        if(param.getName()!=null&&!param.getName().isEmpty()){
            queryWrapper.lambda().like(CamMajor::getName,param.getName());
        }

        //处理查询结果
        List<CamMajor> majorList;
        majorList = baseDao.selectList(queryWrapper);

        return majorList;
    }


    /**
     * 分页查询
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<CamMajor> pageList(Page<CamMajor> page, CamMajor param) {
        return baseDao.pageSelect(page,param.getName());
    }

}
